package com.chushouya.manager.dao.repository;

import com.general.framework.core.lang.Numbers;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.BannerPositionEntity;
import com.chushouya.manager.dao.mapper.BannerPositionMapper;
import com.chushouya.manager.dto.admin.bannerposition.BannerPositionQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Repository
public class BannerPositionRepository extends BaseRepository<BannerPositionMapper, BannerPositionEntity, Long> {

    public PageResult<BannerPositionEntity> selectPage(BannerPositionQuery query) {
        return selectPage(query, buildExample(query));
    }

    public List<BannerPositionEntity> selectList(BannerPositionQuery query) {
        return selectByExample(buildExample(query));
    }

    private Example buildExample(BannerPositionQuery query) {
        Example example = buildExample();
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("delFlag", Numbers.INTEGER_ZERO);
        if (Strings.isNotBlank(query.getPositionName())) {
            c.andLike("positionName", Strings.format("%{}%", query.getPositionName()));
        }
        example.orderBy("sort").asc();
        example.orderBy("positionId").desc();
        return example;
    }
}
